package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Additive;
import cn.edu.scau.cmi.ema.domain.Food;
import cn.edu.scau.cmi.ema.domain.Foodadditive;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("FoodadditiveController")
public class FoodadditiveController extends CmiEmaController {

	@RequestMapping("/listFoodadditives")
	public ModelAndView listFoodadditives(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = foodadditivePaginationService.updatePagination(request, pagination);
		Set<Foodadditive> foodadditives = foodadditivePaginationService.getPaginationFoodadditives(pagination);

		mav.addObject("foodadditives", foodadditives);
		mav.addObject("entityName", "foodadditive");
		mav.addObject("pagination", pagination);
		mav.setViewName("foodadditive/listFoodadditives");
		return mav;
	}

	public String listFoodadditives() {
		return "redirect:/listFoodadditives";
	}

	@RequestMapping("/viewFoodadditive")
	public ModelAndView viewFoodadditive(@RequestParam Integer foodadditiveId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("foodadditive", foodadditiveDAO.findFoodadditiveByPrimaryKey(foodadditiveId));
		mav.setViewName("foodadditive/viewFoodadditive");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteFoodadditives")
	public ModelAndView confirmDeleteFoodadditives(
			@RequestParam(value = "deleteFoodadditivesIds", required = true) Set<Integer> deleteFoodadditivesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Foodadditive> deleteFoodadditives = foodadditiveDAO.findFoodadditivesByIds(deleteFoodadditivesIds);
		Set<Foodadditive> referencedFoodadditives = foodadditiveService.loadReferenceFoodadditives(deleteFoodadditives);
		boolean hasReferenced;
		if (referencedFoodadditives.size() == 0) {
			mav.addObject("deleteEntitys", deleteFoodadditives);
			hasReferenced = false;
		} else {
			deleteFoodadditives.removeAll(referencedFoodadditives);
			mav.addObject("referencedEntitys", referencedFoodadditives);
			mav.addObject("deleteEntitys", deleteFoodadditives);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Foodadditive");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listFoodadditives");
		return mav;
	}

	@RequestMapping("/deleteFoodadditive")
	public String deleteFoodadditive(@RequestParam Integer foodadditiveId) {
		Foodadditive foodadditive = foodadditiveDAO.findFoodadditiveByPrimaryKey(foodadditiveId);
		foodadditiveService.deleteFoodadditive(foodadditive);
		return "forward:/listFoodadditives";
	}

	@RequestMapping("/confirmDeleteFoodadditive")
	public ModelAndView confirmDeleteFoodadditive(@RequestParam Integer foodadditiveId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("foodadditive", foodadditiveDAO.findFoodadditiveByPrimaryKey(foodadditiveId));

		mav.setViewName("foodadditive/deleteFoodadditive");

		return mav;
	}

	@RequestMapping("/deleteFoodadditives")
	public String deleteFoodadditives(
			@RequestParam(value = "deleteFoodadditivesId", required = true) Set<Integer> deleteFoodadditivesId) {
		foodadditiveService.deleteFoodadditives(deleteFoodadditivesId);
		return "forward:/listFoodadditives";
	}

	@RequestMapping("/editFoodadditive")
	public ModelAndView editFoodadditive(@RequestParam Integer foodadditiveId) {

		ModelAndView mav = new ModelAndView();

		Foodadditive foodadditive = foodadditiveDAO.findFoodadditiveByPrimaryKey(foodadditiveId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("foodadditive", foodadditive);

		// 把所有的引用foodadditive对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodadditiveService.loadFoodadditiveObjectReferencedSet(foodadditive));

		// 将编辑页面foodadditive添加到mav中
		mav.setViewName("foodadditive/editFoodadditive");
		return mav;
	}

	@RequestMapping("/newFoodadditive")
	public ModelAndView newFoodadditive() {
		ModelAndView mav = new ModelAndView();
		Foodadditive foodadditive = new Foodadditive();

		mav.addObject("foodadditive", foodadditive);

		// 把所有的引用foodadditive对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(foodadditiveService.loadFoodadditiveObjectReferencedSet(foodadditive));

		mav.addObject("newFlag", true);
		mav.setViewName("foodadditive/editFoodadditive");

		return mav;
	}

	@RequestMapping("/saveFoodadditive")
	public ModelAndView saveFoodadditive(@ModelAttribute Foodadditive foodadditive) {

		ModelAndView mav = new ModelAndView();

		Additive additiveTemp = foodadditive.getAdditive();
		Additive additive = additiveTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(additiveDAO.findAdditiveById(additiveTemp.getId()));
		Food foodTemp = foodadditive.getFood();
		Food food = foodTemp == null ? null : CmiHibernateProxyUtil.deproxy(foodDAO.findFoodById(foodTemp.getId()));

		Foodadditive existingFoodadditive = CmiHibernateProxyUtil.deproxy(foodadditiveDAO
				.findFoodadditiveByPhysicalPrimaryKey(food.getId(), additive.getId(), foodadditive.getContent()));

		foodadditive.setAdditive(additive);
		foodadditive.setFood(food);
		// 新建记录
		if (foodadditive.getId() == null) {
			if (foodadditive.equals(existingFoodadditive)) {

				mav.addAllObjects(foodadditiveService.loadFoodadditiveClassReferencedSet(foodadditive));
				// mav = editContinue(mav, foodadditive);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodadditive/editFoodadditive");
			} else {
				foodadditiveService.saveFoodadditive(foodadditive);
				mav.setViewName("forward:/listFoodadditives");
			}
		}
		// 编辑记录
		else {
			Foodadditive originFoodadditive = CmiHibernateProxyUtil
					.deproxy(foodadditiveDAO.findFoodadditiveByPrimaryKey(foodadditive.getId()));
			if (!foodadditive.equals(originFoodadditive) && foodadditive.equals(existingFoodadditive)) {
				mav.addAllObjects(foodadditiveService.loadFoodadditiveClassReferencedSet(foodadditive));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("foodadditive/editFoodadditive");
			} else {
				foodadditiveService.saveFoodadditive(foodadditive);
				mav.setViewName("forward:/listFoodadditives");
			}
		}
		return mav;
	}

	@RequestMapping("/foodadditiveController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
